<?php
/**
 * 首页
 *
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0
 * @since 2011-06-13
 * @author monkee
 */

class homeApp extends app
{
	function __construct(){
		$this->_init();
		$this->login_need();
	}
	public function index(){
		$feed = & $this->getModel('feed');
		$feeditems = $feed->get_feeds_by_user_id($this->user_id);

		$modmod = $this->getModel('model');
		$modlist = $modmod->get_models_by_user_id($this->user_id, 10, 1);
		$modcount = $modmod->get_models_count_by_user_id($this->user_id);

		$count = $modcount%10;
		
		return array(
			'feeds' => (array)$modlist,
			'count' => $count
		);
	}

	/**
	 * 全能框处理
	 * 全能框是用户在首页中可以快速发布站内其它信息的工具
	 *
	 */
	public function ibody(){
		$message= & $this->getData('post/message');
		$message = trim($message);
		if(empty($message)){
			return array('code' => 0);
		}
		$type = 'doing';
		$doing = & $this->getModel('doing');
		$doing_id = $doing->update($this->user_id, $message);
		//建立model
		$model = & $this->getModel('model');
		$model_id = $model->create($this->user_id, $this->user['username'], $doing_id, 'doing', $message, $message);
		//发布feed
		$feed = & $this->getModel('feed');
		$feedid = $feed->add($this->user_id, $message, $model_id);
		$html = '<li><div class="avatar"><img src="'.get_avatar($this->user_id).'" width="50"></div>';
		$html .= '<div class="feed-body">';
		$html .= '<a href="javascript:void(0);">'.$this->user['username'].'</a>&nbsp;:&nbsp;'.$message.'</div>';
		$html .= '<div class="func" feedid="'.$feedid.'" state="0"><a href="#">加标签</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">分享</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:void(0);" id="a'.$model_id.'" onclick="unfoldComment('.$model_id.')" class="folded">评论</a></div>';
		$html .= '<div class="comment"  id="cts'.$model_id.'"></div>';
		$html .= '</li>';
		return array('code' => 1, 'html' => $html);
	}

	public function replycomment(){
		$data = & $this->getData('post');
		//$data = trim($data);

		if(empty($data)){
			return array('code' => 0);
		}
		if(!$data['commentmsg'] || $data['commentmsg'] == ''){
			return array('code' => 0);
		}
		$data['comID'] = intval($data['comID']);
		$replymod = $this->getModel('reply');
		$r = $replymod->create($this->user_id, $this->user['user_name'], $data['comID'], $data['commentmsg'], 0);
		if($r > 0){
			$modsmall = $this->getModel('model');
			$modsmall->set_reply_count_up($data['comID']);
			echo '1';
			exit;
		}else{
			echo '2';
			exit;
		}
	}


	public function morecomment(){
		$data = $this->getData('post/count_this');
		$count_count = $this->getData('post/count_count');
		$data = intval($data);
		$count_count = intval($count_count);				
		$html = '';
		//意味着不能再加载了
		if($count_count == 0){
			return array('code' => 0);
		}else if($count_count > 0){
			$modmod = $this->getModel('model');
			$lists = $modmod->get_models_by_user_id($this->user_id, $data, 10);		
			foreach($lists as $lii){
				$html .= '<li>';
				$html .= '<div class="avatar"><img src="'.get_avatar($lii[user_id]).'" width="50"></div>';
				$html .= '<div class="feed-body">';
				$html .= '<a href="javascript:void(0);">'.$lii[username].'</a>&nbsp;:&nbsp;'.$lii[message].'</div>';
				$html .= '<div class="func" feedid="'.$lii[feed_id].'" state="0"><a href="#">加标签</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">分享</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:void(0);" id="a'.$lii[model_id].'" onclick="unfoldComment('.$lii[model_id].')" class="folded">评论';
				if($lii['reply_count'] > 0){
					$html .= '('.$lii['reply_count'].')';
				}
				$html .= '</a></div>';
				$html .= '<div class="comment" id="cts'.$lii[model_id].'"></div>';
				$html .= '</li>';
			}						
			return array('code' => 2, 'html' => $html);
		}else{
			return array('code' => 1, 'html' => '');
		}

	}

	public function unfoldcomment(){
		$data = $this->getData('post/ctsID');
		$data = intval($data);
		if(!empty($data)){
			$repmod = $this->getModel('reply');
			$list = $repmod->get_replys($data);

			$html = '<ul>';
			if(!empty($list) || count($list) > 0){
				foreach ($list as $lii){
					$html .= '<li>';
					$html .= '<div class="headimg"><img src="'.get_avatar($lii['user_id']).'"></div>';
					$html .= '<div class="content">';
					$html .= '<p> <a href="">'.$lii['username'].'</a><span>'.date('Y-m-d H:m:s', $lii['mktime']).'</span></p>';
					$html .= '<p>';
					$html .= $lii['message'];
					$html .= '<a href="javascript:void(0);" onclick="replyTo(\''.$lii['model_id'].'\', \''.$lii['username'].'\')">回复</a>';
					$html .= '</p>';
					$html .= '</div>';

					$html .= '</li>';
				}
			}

			$html .= '<li>';
			$html .= '<form action="" method="post" onsubmit="replyCommit(\''.$data.'\', \''.count($list).'\'); return false;">';
			$html .= '<input type="text" id="comment'.$data.'" value="" /><input class="button" type="submit" value="回复"/>';
			$html .= '</form>';
			$html .= '</li>';


			$html .= '</ul>';

			echo $html;
			exit;
			//return array('code' => 1, 'html' => $html);

		}
	}

}
?>